/**
 * 业务运行时序图刻度
 */
define(
	[
		'require'
	],
	function (require) {
		"use strict";
		
		var forEach = Ember.EnumerableUtils.forEach,
			map = Ember.EnumerableUtils.map;
		
		var GraduationView = Ember.Component.extend({
			//间隔5分钟
			interval: 5,

			//模式 默认hour  回溯模式设置为month
			mode: 'hour',
			
			init: function() {
				this._super();
				this.timeChange();
			},
			
			timeChange: Ember.observer("endTime", function() {
				var startTime = this.get('startTime'),
					endTime = this.get('endTime');

				if (startTime && endTime) {
					if (this.$list) {
						this.refresh(startTime, endTime);
						this.get('timePoint') && this.timePointChange();
					}
				}
			}),

			timePointChange: Ember.observer("timePoint", function() {
				var startTime = this.get('startTime'),
					endTime = this.get('endTime'),
					timePoint = this.get('timePoint');

				if (startTime && endTime && timePoint) {
					(!this.$point) && (this.$point = $('<i class="point"></i>').appendTo(this.$list));
					var d = new Date(timePoint),
						pointMinutes = d.getMinutes() + d.getHours() * 60,
						startMinutes = this.timeToMinute(this.get('startTime')),
						endMinutes = this.timeToMinute(this.get('endTime')),
						length = 60;

					(endMinutes < startMinutes) && (endMinutes += 24 * 60);

					if (pointMinutes >= startMinutes && pointMinutes <= endMinutes) {
						this.$point.removeClass('hide').css({left: (pointMinutes - startMinutes) / length * 100 + (50/length) + '%'});
					} else {
						this.$point.addClass('hide');
					}
				} else if (timePoint == null) {
					this.$point&&this.$point.addClass('hide');
				}
			}),
			
			didInsertElement: function() {
				var $wrap = this.$();
				$wrap.width('100%');
				this.$list = $('<div class="scale-list"></div>').appendTo($wrap);
				this.timeChange();
			},
			
			willDestroyElement: function() {
				
			},
			
			refresh: function(startTime, endTime) {
				switch(this.mode) {
					case 'month':
						this.refreshMonth(startTime, endTime);
						break;
					case 'hour':
					default:
						this.refreshHour(startTime, endTime);
						break;
				}				
			},

			refreshHour: function(startTime, endTime) {
				var me = this,
					$list = this.$list,
					timeData = this.getTimeData(startTime, endTime),
					lastIndex = timeData.length - 1,
					length = 60,
					min = this.timeToMinute(this.get('startTime'));
					if(me.get("length")){
						length = 30;
					}
				$list.find('span').remove();

				forEach(timeData, function(x) {
					var $item = $('<span></span>').appendTo($list);
					$item.css({left: (50/length*(2*(x-min)+1) - 2.5) + '%'});
					$item.text(me.minuteToTime(x));
				});

				/*((timeData[1]-min) < 3) && $list.children('span').eq(1).addClass('hide-line');
				(timeData[lastIndex] - timeData[lastIndex-1] < 3) && $list.children('span').eq(lastIndex-1).addClass('hide-line');*/
			},
			
			refreshMonth: function(startTime, endTime) {
				var $list = this.$list,
					month = new Date(endTime).getDate() + 1,
					day = new Date(endTime).getDate(),
					scale = (24*(day-1)+23) / 24;
				
				$list.html('');
				for (var i = 1; i <= day; i++) {
					var $item = $('<span></span>').appendTo($list);
					$item.css({left: (100/scale*(i-1) - 2.5) + '%'});
					$item.text(i);
				}
			},

			getTimeData: function(startTime, endTime) {
				var start = this.timeToMinute(startTime),
					end = this.timeToMinute(endTime),
					interval = this.interval,
					arr = [];
				
				//0点过度时候结束时间小于开始时间,修正
				(end < start) && (end += 24 * 60);
				
				for (var value = (Math.floor(start / interval) + 1) * interval; value <= end; value += interval) {
					arr.push(value);
				}

				(start % interval == 0) && arr.unshift(start);
				
				/*(arr[0] > start) && arr.unshift(start);
				(arr[arr.length - 1] < end) && arr.push(end);*/
				
				return arr;
			},
			
			timeToMinute: function(time) {
				return this.sumMinute.apply(this, time.split(':'));
			},
			
			sumMinute: function(h, m) {
				return parseInt(h, 10) * 60 + parseInt(m, 10);
			},
			
			minuteToTime: function(minute) {
				var remainder = minute % 60;
				return this.fillZero((minute - remainder) / 60 % 24) + ':' + this.fillZero(remainder);
			},
			
			fillZero: function(num) {
				return num < 10 ? ('0' + num) : num;
			}
		});
		
		APM.ui.Graduation = GraduationView;
		
		return GraduationView;
	}
)